package com.markus.code.exam.duxiaoman;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/20 20:36
 * todo:
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T != 0){
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            char [][]matrix = new char[n][m];
            int beginRow = 0;
            int beginCol = 0;
            for (int i = 0;i<n;i++){
                String str = scanner.next();
                char []chars = str.toCharArray();
                for (int j=0;j<m;j++){
                    if (chars[j] == '@'){
                        beginRow = i;
                        beginCol = j;
                    }
                    matrix[i][j] = chars[j];
                }
            }
            boolean[][] visited = new boolean[n][m];
            List<Integer> list = new ArrayList<>();
            process(matrix,beginRow,beginCol,matrix.length,matrix[0].length,0,visited,list);
            int ans = Integer.MAX_VALUE;
            for (Integer integer : list) {
                ans = Math.min(ans,integer);
            }
            System.out.println(ans == Integer.MAX_VALUE?-1:ans);
            T--;
        }
    }
    private static void process(char[][] matrix, int row, int col, int rows, int cols, int use, boolean[][] visited, List<Integer> list){
        if (row < 0 || row >= rows || col < 0 || col >= cols){
            list.add(use);
            return ;
        }
        if (visited[row][col]){
            return ;
        }
        if (matrix[row][col] == '#'){
            return ;
        }
        if (matrix[row][col] == '*'){
            use ++;
        }
        visited[row][col] = true;
        process(matrix,row+1,col,rows,cols,use,visited,list);
        process(matrix,row-1,col,rows,cols,use,visited,list);
        process(matrix,row,col-1,rows,cols,use,visited,list);
        process(matrix,row,col+1,rows,cols,use,visited,list);
        visited[row][col] = false;
    }
}
